package cn.org.dian.easycommunicate.xml;

import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import cn.org.dian.easycommunicate.model.Content;
import cn.org.dian.easycommunicate.model.Term;
import cn.org.dian.easycommunicate.model.Theme;


public class XmlParser {

    /**
     * 
     * @param inputStream
     * @return the loaded Content object 
     */
	public static Content readContentfromXML(InputStream inputStream) {

		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser saxParser = spf.newSAXParser();

			XMLContentHandler handler = new XMLContentHandler();
			saxParser.parse(inputStream, handler);

			return handler.getContent();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}

class XMLContentHandler extends DefaultHandler {

	// for constants
	private static final String TAG_TERM = "term";
	private static final String TAG_THEME = "theme";
	private static final String TAG_LANGUAGE = "language";
	private static final String ATTR_NAME = "name";

	private Content content = null;

    private Theme currentTheme = null;
    
    private Term currentTerm = null;

	private String currentLanguage = null;
	
	private boolean hasLanguageNamesAdded = false;
	
	private boolean isAdding = false;

	@Override
	public void startDocument() throws SAXException {
		this.content = new Content();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
	    String attsString = atts.getValue(ATTR_NAME);
		if (TAG_THEME.equals(localName)) {
			this.currentTheme = new Theme();
			this.currentTheme.setName(attsString);
			Theme.addThemeName(attsString);
		} else if (TAG_TERM.equals(localName)) {
			this.currentTerm = new Term();
			this.currentTerm.setName(attsString);
			if (!hasLanguageNamesAdded) {
			    isAdding = true;
			}
		} else if (TAG_LANGUAGE.equals(localName)) {
		    this.currentLanguage = attsString;
		    if (isAdding) {
		        Term.addAllLanguageNames(attsString);
		    }
		} else {

		}

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if (TAG_LANGUAGE.equals(localName)) {
			this.currentLanguage = null;
		} else if (TAG_TERM.equals(localName)) {
		    this.currentTheme.addTerm(currentTerm.getName(), currentTerm);
		    if (isAdding) {
		        isAdding = false;
		        hasLanguageNamesAdded = true;
		    }
		} else if (TAG_THEME.equals(localName)) {
		    this.content.addTheme(currentTheme.getName(), currentTheme);
		}
		
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (null != this.currentLanguage) {
		    String content = new String(ch, start, length);
		    this.currentTerm.addLanguage(this.currentLanguage, content);
		}
	}

	public Content getContent() {
        return this.content;
    }

}